package com.zhouyu.mongo.service;

import com.zhouyu.mongo.dao.MongoDemoDao;
import com.zhouyu.mongo.entity.ExamEntity;
import com.zhouyu.mongo.entity.ExamPaper;
import com.zhouyu.mongo.entity.ExamQuestion;
import com.zhouyu.mongo.entity.ExamUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author:zhouyu
 * @create: 22:25 2019/11/7
 * @Description: service
 */
@Service
public class MongoService {
    @Autowired
    private MongoDemoDao mongoDemoDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    public void save(){
        LocalDateTime localDateTime = LocalDateTime.now(ZoneOffset.of("+8"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String dateTime = localDateTime.format(formatter);
        ExamEntity examEntity = new ExamEntity();
        examEntity.setId(UUID.randomUUID().toString());
        examEntity.setExamId("9f81bc1-c324-4ea2-9492-e20e1bd7ab7d");
        examEntity.setExamRecordId("9d5b9e59-7f2d-47c2-b93e-967431409c9f");
        examEntity.setSubmitTime(dateTime);
        examEntity.setDealTime(new Date());
        examEntity.setAnsweredCount(1);
        examEntity.setAnswerRecords("[{\"questionId\":\"91c5b128-e6a3-4fa9-aded-3c82aa738cde\",\"answer\":\"0,1,2\"},{\"questionId\":\"ed4cd2ff-3c68-4bd5-b0aa-6d1df286941b\",\"answer\":\"1,3,0\"},{\"questionId\":\"13760a61-c48f-4901-ba57-577f119c5a14\",\"answer\":\"2,1,3\"}]");
        examEntity.setFullAnswerRecords("[{\"questionId\":\"db2f17d0-3ce9-49c8-9ea9-9e13d68d49c5\",\"answer\":\"1\",\"answerImageList\":null},{\"questionId\":\"6b40af3d-9acd-40c6-9ab0-2ee9006716f3\",\"answer\":\"2\",\"answerImageList\":null},{\"questionId\":\"acbf9602-c121-444a-ac39-a3c38b823fa1\",\"answer\":\"1\"},{\"questionId\":\"e8f9fd93-548d-4c5d-9365-e75dd9b4abe1\",\"answer\":\"3\"},{\"questionId\":\"91c5b128-e6a3-4fa9-aded-3c82aa738cde\",\"answer\":\"0,1,2\"},{\"questionId\":\"ed4cd2ff-3c68-4bd5-b0aa-6d1df286941b\",\"answer\":\"1,3,0\"},{\"questionId\":\"13760a61-c48f-4901-ba57-577f119c5a14\",\"answer\":\"2,1,3\"}]");
        mongoDemoDao.save(examEntity);
    }

    /**
     * 分页查询
     * @param start
     * @param size
     * @param examId
     * @param userId
     */
    public void pageQuery(int start,int size,String examId,String userId){
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("examId").is(examId).and("userId").is(userId);
        query.addCriteria(criteria);
        long total = mongoTemplate.count(query, ExamEntity.class);
        query.skip(start);
        query.limit(size);
        List<ExamEntity> examEntities = mongoTemplate.find(query, ExamEntity.class);
        System.out.println("pagesize:"+examEntities.size());
        System.out.println("total:"+total);
    }

    /**
     * 级联插入
     */
    public void saveExamPaper(){
        ExamUser examUser = new ExamUser();
        examUser.setSex("男");
        examUser.setName("周宇");
        ArrayList<ExamQuestion> examQuestions = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setName("问题"+i);
            examQuestion.setType(i);
            examQuestions.add(examQuestion);
        }
        ExamPaper examPaper = new ExamPaper();
        examPaper.setCreateTime(LocalDateTime.now());
        examPaper.setId(UUID.randomUUID().toString());
        examPaper.setType(1);
        examPaper.setName("试卷1");
        examPaper.setExamQuestion(examQuestions);
        examPaper.setExamUser(examUser);
        mongoTemplate.save(examPaper);
    }

    /**
     * 添加级联属性
     */
    public void addFeild(){
        Query query = new Query(Criteria.where("_id").is("f29ab488-ba30-4367-826f-352aa5d3f171")
                                        .and("examQuestion.type").is(4));
        //添加属性(更新属性)
        Update update = new Update().set("examQuestion.$.order", 4);
        mongoTemplate.updateFirst(query,update,ExamPaper.class);
        ExamPaper examPaper = mongoTemplate.findOne(query, ExamPaper.class);
        System.out.println(examPaper);
        //删除属性
        //Update update2 = new Update().unset("examQuestion.$.order");
        //mongoTemplate.updateFirst(query,update2,ExamPaper.class);
        Query query2 = new Query(Criteria.where("_id").is("f29ab488-ba30-4367-826f-352aa5d3f171")
                                        .and("examQuestion.order").is(4));
        ExamQuestion examQuestion = mongoTemplate.findOne(query2, ExamQuestion.class);
        System.out.println(examQuestion);
    }

}
